package com.restaurant.application.minifacade.data;

import com.google.common.collect.Lists;
import com.nebutown.generation.Convert;
import com.nebutown.generation.PickableBean;

import java.util.List;

public class PhoneBO implements PickableBean {

    public static final String FN_PHONE = "phone";
    public static final String FN_CODE = "code";

    public static final List<String> FIELD_NAMES = Lists.newArrayList(
            FN_PHONE,
            FN_CODE);

    private String phone;
    private String code;

    public PhoneBO(String phone, String code) {
        setPhone(phone);
        setCode(code);
    }

    public PhoneBO() {
    }

    public PhoneBO duplicate() {
        PhoneBO result = new PhoneBO();
        result.phone = this.phone;
        result.code = this.code;
        return result;
    }

    @Override
    public String toString() {
        return Convert.toJson(this);
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String value) {
        this.phone = value;
    }

    public PhoneBO withPhone(String value) {
        setPhone(value);
        return this;
    }

    public PhoneBO withPhone(String value, String defaultValue) {
        setPhone(value == null ? defaultValue : value);
        return this;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String value) {
        this.code = value;
    }

    public PhoneBO withCode(String value) {
        setCode(value);
        return this;
    }

    public PhoneBO withCode(String value, String defaultValue) {
        setCode(value == null ? defaultValue : value);
        return this;
    }

    public Object getValueOf(String name) {
        switch (name) {
            case "phone":
                return getPhone();
            case "code":
                return getCode();
        }
        return null;
    }

    public boolean hasField(String name) {
        return FIELD_NAMES.contains(name);
    }

    @Override
    public int hashCode() {
        return signature().hashCode();
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof PhoneBO)) return false;
        return signature().equals(((PhoneBO) o).signature());
    }

    public String signature() {
        throw new RuntimeException("Bean PhoneBO has no signature defined, no hash code or equals possible.");
    }
}
